'''
Created on 2009-11-20

@author: yuchen
This unittest is for testing whether the successor are correct calculated when some certain move
is applied by the opponent player, which including:

testSuccesorStateMoves:
	inherited the states from testStateSuccessorMove

testOpponentMoves:
	for the human player
'''
import unittest, copy
import gamemodel.board
import gamemodel.player
import gamemodel.state
import gamemodel.piece
import interface.ConsoleAgent
import ai.computer
import ai.human
import game_model_test
import gamephase.computerVsComputer

class Test(unittest.TestCase):
	__board_for_test = None
	__player0 = None
	__player1 = None

	def testSuccesorStateMoves(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		for successorState in state.Successor() :
			successorState.switchPlayer()
			# use either one of these two commands below
			moves = successorState.successorMove()
			afterSuccessorState = successorState.Successor()
			
#		    if need, use these commands below to trace the process
#			print "One of the moves that the current player chooses"
#			print
#			interface.ConsoleAgent.ConsoleDescriptor.drawState(successorState)
#		
#			for number in range(len(moves)):				
#				print "one of the moves that the opponent player chooses"
#				print
#				interface.ConsoleAgent.ConsoleDescriptor.drawState(afterSuccessorState[number])
#				print
#				print "original for opponent player is", moves[number].list[0].original
#				print "destination for opponent is", moves[number].list[0].destination
#				pieces = afterSuccessorState[number].board.getPlayer1().pieces
#				print "the list of opponent piece is:"
#				for piece in pieces:
#					print piece.position,
#				print
#				print "one branch end!"
#				print
#				print	
			
			self.assertEqual(len(moves), 9)
		return
	
	def testOpponentMoves(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		moves = state.successorMove()
		
		#First Part, this just demonstrate how to test, no error for this part
		pieces = ai.human.AIHuman.getMovablePieces(moves)
		piece = (1,3)
		self.assertTrue(piece in pieces)
		destinations = ai.human.AIHuman.getAvailableDestinations(moves, piece)
		self.assertTrue((0,4) in destinations)
		micro_move = gamemodel.state.MicroMove(1, 3, 0, 4)
		self.assertTrue(game_model_test.Test.isMicroMoveInCheckerMoves(micro_move, moves))
				
		#Second Part, this is strange....
		state = state.take_micro_move(micro_move)
		
#       the position of (1,3) to player0 is empty now
#		self.assertTrue(state.board.isEmpty(1, 3,state.getCurrentPlayer()))

#       just for making sure the pieces	lists of the two players	
#		print			
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)	
#		for piece in state.getCurrentPlayer().pieces :
#			print state.getCurrentPlayer()
#			print piece.position,
#		print
#		for piece in state.getPlayers()[0].pieces :
#			print state.getPlayers()[0]
#			print piece.position,
#		print
#		for piece in state.getPlayers()[1].pieces :
#			print state.getPlayers()[1]
#			print piece.position,
#		print
#		self.assertNotEqual(state.getPlayers()[1].pieces, state.getPlayers()[0].pieces)
		
		state.switchPlayer()
		moves = state.successorMove()
		pieces = ai.human.AIHuman.getMovablePieces(moves)

#       As you can see, the position (1,3) of player1 is not empty
#		print "have changed player"
#		self.assertFalse(state.board.isEmpty(1, 3,state.getCurrentPlayer()))
		
		piece = (0,2)
		self.assertFalse(piece in pieces)
		self.assertTrue(game_model_test.Test.isMicroMoveInCheckerMoves(micro_move, moves))
		
		return
#	
#	def testComputerVsComputer(self):
#		gamephase.computerVsComputer.Play()
#		return


if __name__ == "__main__":
	#import sys;sys.argv = ['', 'Test.testName']
	unittest.main()